package com.example.controller;

import com.example.Utils.LoggerUtils;
import com.example.model.Menu;
import com.example.model.Restaurant;
import com.example.model.Result;
import com.example.modelDTO.MenuDTO;
import com.example.modelDTO.converter.MenuConverter;
import com.example.service.IMenuService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Hello
 * @since 2024-07-13
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    IMenuService iMenuService;
    @Autowired
    MenuConverter menuConverter;
    private static final Logger logger = LoggerUtils.getLogger(MenuController.class);
    @PostMapping("/getMenu")
    public Result<List<MenuDTO>> getMenu(@RequestBody Menu menu){

        Integer restaurantId = menu.getRestaurantId();
        logger.info(String.valueOf(restaurantId));
        List<Menu> menus = iMenuService.getMenuByRestaurantId(restaurantId);
        List<MenuDTO> menuDTOs = new ArrayList<>();
        for (Menu menu1 : menus) {
            // logger.info("DishName:{}",menu1.getDishName());
            menuDTOs.add(menuConverter.sourceToTarget(menu1));
        }
        return Result.success(menuDTOs);
    }
    @PostMapping("/getDish")
    public Result<Menu> getDish(@RequestBody Menu menu){

        Menu dish = iMenuService.getDish(menu);
        MenuDTO menuDTO = menuConverter.sourceToTarget(dish);
        return dish == null ? Result.fail("失败") : Result.success(menuDTO);
    }
}
